/**
 * 定义PLC系统报警模块
 */
(function(window, angular, undefined){
    var module = angular.module('water.apps.sw.bdPlcControl.system.bdPlcSystemAlarm', ['water.entity.bdPlcControl',
        'water.entity.bcMonitorParamDict',
        'water.entity.protocol',
        'water.common.dateUtil']);

    module.controller('bdPlcSystemAlarmController',function($scope,$stateParams,$dateUtil,
                                                            $bdPlcControlService,
                                                            $bcMonitorParamDictService){
        /**
         * 报警状态选择集
         */
        $scope.alarmStatusSelections = [
            {"k":"all","v":"全部"},
            {"k":"0","v":"正在报警"},
            {"k":"1","v":"报警结束"}];

        /**
         * 获取报警参数
         */
        $scope.getParamData = function () {
            $bcMonitorParamDictService.getList().then(function (data) {
                var paramData = [];
                var obj = {};
                obj.k = "all";
                obj.v = "全部";
                paramData.push(obj)
                for (var i = 0; i < data.length; i++) {
                    paramData.push({
                        k: data[i].PARAM_CODE_BUS,
                        v: data[i].PARAM_NAME
                    });
                }
                $scope.paramData = paramData;
                $scope.paramDataModel.setData(paramData);
            });
        };

        /**
         * 获取系统PLC报警
         */
        $scope.getPlcAlarm = function(){
            var startTimeStr =$scope.startDate + " 00:00:00";
            var endTimeStr = $scope.endDate + " 23:59:59";

            var start = new Date(Date.parse(startTimeStr));
            var end = new Date(Date.parse(endTimeStr));

            if(start>end){
                alert("开始时间不能大于结束时间");
                return;
            }

            $scope.queryObj = {};
            $scope.queryObj.PARAM_CODE_BUS = $scope.PARAM_CODE_BUS;
            $scope.queryObj.ALARM_STATUS = $scope.alarmStatus;
            $scope.queryObj.START = startTimeStr;
            $scope.queryObj.END = endTimeStr;

            $bdPlcControlService.getPlcAlarm($scope.queryObj).then(function(data){
                data.forEach(function (data) {
                    /**
                     * 报警状态翻译
                     */
                    $scope.alarmStatusSelections.forEach(function (item) {
                        if (data.ALARM_STATUS === item.k) {
                            data.ALARM_STATUS_SHOW = item.v;
                        }
                    });
                });
                $scope.data = data;
            });
        }

        $scope._init = function(){
            //$scope.queryObj = $stateParams.data;
            $scope.PARAM_CODE_BUS = "system";
            $scope.alarmStatus = "all";
            var date = new Date();
            $scope.startDate = $dateUtil.formatDate(date,'yyyy-MM-dd');
            $scope.endDate = $dateUtil.formatDate(date,'yyyy-MM-dd');
            $scope.getParamData();
            $scope.getPlcAlarm();
        }

        $scope._init();
    });

}(window, angular));


